/// <reference path="../base.js" />
/// <reference path="./sector.js" />
/// <reference path="../data/constants.js" />

/* Utility scripts for Pardus Sector Tool
 * (C) Yassen 2010
 *
 * object.js
 * Defines functions for getting/setting object information.
 *
 * Functions:
 * array   GetList(object)
 * void    SetList(object, list)
 * int     GetIndex(object, id)
 * void    Add(object, data)
 * void    Update(object, data)
 * void    Remove(object, id)
 * bool    Exists(object, id)
 * Object  Get(object, id)
 *
 */

Utility.Object.GetList = function(object, sectorID)
{
  /// <summary>Gets the sector's list of objects.</summary>
  /// <param name="object" type="eObject">Object to get the list for.</param>
  /// <param name="sectorID" type="Int">(Optional) Sector ID of the sector to look up. Defaults to CurrentSector</param>
  /// <returns type="Array">The List</returns>

  if (sectorID == undefined)
    sectorID = Global.CurrentSector;

  switch (object)
  {
    case eBuilding: return Global.SectorList[sectorID].buildings;
    case ePlanet: return Global.SectorList[sectorID].planets;
    case eStarbase: return Global.SectorList[sectorID].starbases;
    case eBackground: return Global.SectorList[sectorID].backgrounds;
    default: return undefined;
  }
}

Utility.Object.SetList = function(object, list, sectorID)
{
  if (sectorID == undefined)
    sectorID = Global.CurrentSector;

  switch (object)
  {
    case eBuilding: Global.SectorList[sectorID].buildings = list; break;
    case ePlanet: Global.SectorList[sectorID].planets = list; break;
    case eStarbase: Global.SectorList[sectorID].starbases = list; break;
    case eBackground: Global.SectorList[sectorID].backgrounds = list; break;
  }
}

Utility.Object.GetIndex = function(object, id)
{
  var objectList = Utility.Object.GetList(object);

  for(var i = 0; i < objectList.length; i++)
    if(objectList[i].id == id)
      return i;

  return undefined;
}

Utility.Object.Add = function(object, data)
{
  var objectList = Utility.Object.GetList(object);
  objectList.push(data);
  Utility.Object.SetList(object, objectList);
}

Utility.Object.Update = function(object, id, data)
{
  var objectList = Utility.Object.GetList(object);
  objectList[Utility.Object.GetIndex(object, id)] = data;
  Utility.Object.SetList(object, objectList);
}

Utility.Object.Delete = function(object, id)
{
  if (object == eNull)
    object = Utility.Object.GetType(id);

  var objectList = Utility.Object.GetList(object);
  objectList.splice(Utility.Object.GetIndex(object, id), 1);
  Utility.Object.SetList(object, objectList);
}

Utility.Object.Exists = function(object, id)
{
  if (object == eNull)
    return Utility.Object.GetType(id) != eNull;

  else
    return Utility.Object.GetIndex(object, id) != undefined;
}

Utility.Object.Get = function(object, id)
{
  if (object == eNull)
    object = Utility.Object.GetType(id);
    
  return Utility.Object.GetList(object)[Utility.Object.GetIndex(object, id)];
}

Utility.Object.GetType = function(id)
{
  if (Utility.Object.GetIndex(eBuilding, id) != undefined)
    return eBuilding;

  else if (Utility.Object.GetIndex(ePlanet, id) != undefined)
    return ePlanet;

  else if (Utility.Object.GetIndex(eStarbase, id) != undefined)
    return eStarbase;

  else if (Utility.Object.GetIndex(eBackground, id) != undefined)
    return eBackground;

  else
    return eNull;
}